using B.S.Data.MES.API.Applications.WorkOrderManagement.Commands;
using B.S.Domain.Ticket;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.WorkOrderManagement.CommandHandlers
{
    /// <summary>
    /// 获取所有工单命令处理器
    /// </summary>
    public class GetAllWorkOrdersCommandHandler : IRequestHandler<GetAllWorkOrdersCommand, APIRequst<List<WorkOrder>>>
    {
        private readonly IBaseRepository<WorkOrder> _workorderRepository;

        public GetAllWorkOrdersCommandHandler(IBaseRepository<WorkOrder> workorderRepository)
        {
            _workorderRepository = workorderRepository;
        }

        /// <summary>
        /// 处理获取所有工单的请求
        /// </summary>
        /// <param name="request">查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>工单列表</returns>
        public async Task<APIRequst<List<WorkOrder>>> Handle(GetAllWorkOrdersCommand request, CancellationToken cancellationToken)
        {
            var workOrders = _workorderRepository.GetAll()
                .ToList();

            return new APIRequst<List<WorkOrder>>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString(),
                Data = workOrders
            };
        }
    }
}